
//************* POLYNOMIAL OPERATIONS *******************
// GAURAV DESHMUKH

#include<iostream.h>
#include<conio.h>
#include<math.h>


class node
{
	int exp,base;
	class node *next;

	public:
	friend class polynom;
};

class polynom
{
	class node *head;

	public:
	polynom()
	{
		head=new(node);
		head->exp=0;
		head->base=0;
		head->next=head;
	}

	void create(void);
	void add(polynom);
	void multiply(polynom);
	void evaluate(void);

	void chdisplay(void);


};

int main(void)
{
	int chmnu;
	class polynom p1,p2;

	while(1)
	{
		clrscr();
		cout<<"\t\t\tP O L Y N O M I A L S";
		cout<<"\n\t\t-----------------------------------";
		cout<<"\n\n1.CREATE NEW SET OF POLYNOMIALS\n\n";
		cout<<"2.ADD TWO POLYNOMIALS\n\n3.MULTIPLY TWO POLYNOMIALS";
		cout<<"\n\n4.EVALUATE A POLYNOMIAL\n\n5.EXIT";

		cout<<"\n\n\nEnter Your Choice:-\t";
		cin>>chmnu;

		switch(chmnu)
		{
			case 1: clrscr();
				cout<<"ENTER FIRST POLYNOMIAL:\n\n";
				p1.create();

				clrscr();
				cout<<"ENTER SECOND POLYNOMIAL:\n\n";
				p2.create();

				break;

			case 2: p1.add(p2);
				break;

			case 3: p1.multiply(p2);
				break;

			case 4: clrscr();
				cout<<"ENTER A POLYNOMIAL TO EVALUATE:";
				p2.create();
				cout<<"\n\n\nTHIS POLYNOMIAL IS SAVED AS 2nd POLYNOMIAL";
				p2.evaluate();
				break;

			case 5: return 0;

			default: cout<<"\n\n\aWRONG I/P";
		}

		getch();
	}
}


//*********CREATING A NEW POLYNOMIAL****************

void polynom::create(void)
{
	char chcreate='+';
	class node *temp;

	//ALLOCATING MEMORY FOR A NEW LINK LIST

	cout<<"\n\nENTER A POLYNOMIAL OF ANY LENGTH";
	cout<<"\n\nenter base,exponent & if if u wanna add more terms ";
	cout<<"then press \'+\' else press any other character";
	cout<<"\n\nEVEN IF ENTER IS PRESSED BY MISTAKE YOU CAN CARRY ON BY ";
	cout<<"PRESSING +\n\n\n\n";


	cout<<"\n\nENTER POLYNOMIAL (base exp +):-\t";


	temp=new(node);

	head->next=temp;

	do
	{
		cin>>temp->base>>temp->exp>>chcreate;
		temp->next=head;


		if(chcreate=='+')
		{
			temp->next=new(node);
			temp=temp->next;
		}

	}while(chcreate=='+');

}

//*********************DISPLAY POLYNOMIAL**********************
void polynom::chdisplay(void)
{
	class node *check,*disnode,*previous;
	int cnt=0;

	disnode=head->next;


	//CHECKING FOR SAME EXPONENTS MULTIPLE TIMES
	for(previous=head,check=head->next;check!=head && head->base==0;check=check->next)
	{
		if(check->base==0)
		{
			previous->next=check->next;
			delete(check);
			goto endfstfor;
		}

		for(previous=check,disnode=check->next;disnode!=head;)
		{
			if(check->exp == disnode->exp)
			{
				check->base= check->base + disnode->base;

				previous->next=disnode->next;
				delete(disnode);
				disnode=previous->next;
			}

			else
			{
				previous=disnode;
				disnode=disnode->next;
			}
		}
		endfstfor:
		previous=check;
	}

	head->base=1;
	//DISPLAYING A POLYNOMIAL

	disnode=head->next;

	if(disnode==head)
	{
		cout<<"0";
		return;
	}


	while(disnode!=head)
	{
		cout<<disnode->base<<"X^"<<disnode->exp;

		if(disnode->next!=head)
			cout<<" + ";

		disnode=disnode->next;
		cnt++;
		if(cnt==9)
		cout<<"\n";
	}


}

//*********************ADD TWO POLYNOMIALS **********************
void polynom::add(class polynom poly2)
{
	class polynom addpoly;
	class node *adnode,*temp;

	clrscr();

	cout<<"THE FIRST POLYNOMIAL IS:\n\n";
	chdisplay();

	cout<<"\n\nTHE SECOND POLYNOMIAL IS:\n\n";
	poly2.chdisplay();

	adnode=head->next;
	temp=new(node);

	addpoly.head->next=temp;

	while(adnode!=head)
	{
		temp->exp=adnode->exp;
		temp->base=adnode->base;

		adnode=adnode->next;

		temp->next=new(node);
		temp=temp->next;

	}

	adnode=poly2.head->next;

	while(adnode!=poly2.head)
	{
		temp->exp=adnode->exp;
		temp->base=adnode->base;

		adnode=adnode->next;

		if(adnode!=poly2.head)
		{
			temp->next=new(node);
			temp=temp->next;
		}
	}

	temp->next=addpoly.head;

	cout<<"\n\n\n\nTHE SUM OF THE POLYNOMIALS IS:\n\n";
	addpoly.chdisplay();


	//DELETING ALLOTED NODES

	adnode=addpoly.head->next;
	while(adnode!=addpoly.head)
	{
		addpoly.head->next=adnode->next;
		delete(adnode);
		adnode=addpoly.head->next;
	}
	delete(addpoly.head);

	cout<<"\n\n\nBOTH POLYNOMIALS ARE UNCHANGED";

}

//***************MULTIPLY POLYNOMIALS **************
void polynom::multiply(class polynom poly2)
{
	class polynom mulpoly;
	class node *mulnode1,*mulnode2,*temp;

	clrscr();

	cout<<"THE FIRST POLYNOMIAL IS:\n\n";
	chdisplay();

	cout<<"\n\nTHE SECOND POLYNOMIAL IS:\n\n";
	poly2.chdisplay();

	temp=new(node);
	mulpoly.head->next=temp;

	mulnode1=head->next;

	while(mulnode1!=head)
	{
		mulnode2=poly2.head->next;

		while(mulnode2!=poly2.head)
		{
			temp->exp=mulnode1->exp+mulnode2->exp;
			temp->base=mulnode1->base * mulnode2->base;

			if(mulnode1->next!=head || mulnode2->next!=poly2.head)
			{
				temp->next=new(node);
				temp=temp->next;
			}

			mulnode2=mulnode2->next;
		}
		mulnode1=mulnode1->next;
	}

	temp->next=mulpoly.head;


	cout<<"\n\n\n\nTHE PRODUCT OF THE POLYNOMIALS IS:\n\n";
	mulpoly.chdisplay();

	//DELETING ALLOTED NODES

	mulnode1=mulpoly.head->next;
	while(mulnode1!=mulpoly.head)
	{
		mulpoly.head->next=mulnode1->next;
		delete(mulnode1);
		mulnode1=mulpoly.head->next;
	}
	delete(mulpoly.head);

	cout<<"\n\n\nBOTH POLYNOMIALS ARE UNCHANGED";
}

//*********************EVALUATE A POLYNOMIAL****************
void polynom::evaluate()
{
	int x;
	long anseval=0;
	class node *evalnode;

	clrscr();
	cout<<"THE POLYNOMIAL GONNA BE EVALUATED IS:\n\n";
	chdisplay();

	cout<<"\n\n\nENTER VALUE OF VARIABLE \"X\":-\t";
	cin>>x;

	evalnode=head->next;

	while(evalnode!=head)
	{
		anseval=anseval+ (evalnode->base)* (pow(x,evalnode->exp));
		evalnode=evalnode->next;
	}

	cout<<"\n\nTHE ANSWER IS :-\t"<<anseval;

}
